package cn.dgut.training.controller;


import cn.dgut.training.annotation.LoginUser;
import cn.dgut.training.dto.goods.CategoryDTO;
import cn.dgut.training.entity.goods.Category;
import cn.dgut.training.service.ICategoryService;
import cn.dgut.training.vo.BaseVO;
import cn.dgut.training.vo.goods.CategoryVO;
import cn.dgut.training.vo.CommonResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private ICategoryService categoryService;


    /**
     * 创建新的分类 第一级分类的父id为0
     * @param categoryDTO
     * @return
     */
    @PostMapping("/create")
    public CommonResponse createCategory(@RequestBody @Valid CategoryDTO categoryDTO){
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.save(category);
        BaseVO<Category> categoryBaseVO = new BaseVO<>();
        List<Category> categoryList=new ArrayList<>();
        categoryList.add(category);
        categoryBaseVO.setList(categoryList);
        return CommonResponse.success(categoryBaseVO);
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/update")
    public CommonResponse updateCategory(@RequestBody @Valid CategoryDTO categoryDTO){
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.updateById(category);
        BaseVO<Category> categoryBaseVO = new BaseVO<>();
        List<Category> categoryList=new ArrayList<>();
        categoryList.add(category);
        categoryBaseVO.setList(categoryList);
        return CommonResponse.success(categoryBaseVO);
    }

    /**
     *列出全部一级分类
     * @return
     */
    @PostMapping("/list")
    public CommonResponse listFirstCategory(){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",0);
        List<Category> categoryList=categoryService.list(queryWrapper);
        List<CategoryVO> categoryVOList = categoryList.stream().map(category -> {
            List<Category> categoryChildrenList = categoryService.list(new QueryWrapper<Category>().eq("pid",category.getId()));
            List<CategoryVO> categoryVOChildrenList = categoryChildrenList.stream().map(category1 -> {
                CategoryVO categoryVO1=new CategoryVO();
                BeanUtils.copyProperties(category1,categoryVO1);
                return categoryVO1;
            }).collect(Collectors.toList());
            CategoryVO categoryVO=new CategoryVO();
            BeanUtils.copyProperties(category,categoryVO);
            categoryVO.setChildren(categoryVOChildrenList);
            return categoryVO;
        }).collect(Collectors.toList());
        BaseVO<CategoryVO> categoryBaseVO = new BaseVO<>();
        categoryBaseVO.setList(categoryVOList);
        return CommonResponse.success(categoryBaseVO);
    }

    /**
     * 内部类-报错一级目录的id以及名称
     */
    class CategoryFVO{
        public Long value;
        public String label;
    }

    /**
     * 列出一级类目
     *
     * @return
     */
    @GetMapping("/l1")
    public CommonResponse listFirst() {
        List<CategoryFVO> categoryFVOS = new ArrayList<>();
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", 0);
        List<Category> categoryList = categoryService.list(queryWrapper);
        for (Category category : categoryList) {
            CategoryFVO categoryFVO = new CategoryFVO();
            categoryFVO.value = category.getId();
            categoryFVO.label = category.getName();
            categoryFVOS.add(categoryFVO);
        }
        BaseVO<CategoryFVO> baseVO = new BaseVO<>();
        baseVO.setList(categoryFVOS);
        return CommonResponse.success(baseVO);
    }
    /**
     * 分类上下架
     * @param categoryDTO
     * @return
     */
    @PostMapping("/delete")
    public CommonResponse upperOrLowerCategory(@RequestBody CategoryDTO categoryDTO){
        Category category=categoryService.getById(categoryDTO.getId());
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.removeById(category.getId());
        BaseVO<Category> categoryBaseVO = new BaseVO<>();
        List<Category> categoryList=new ArrayList<>();
        categoryList.add(category);
        categoryBaseVO.setList(categoryList);
        return CommonResponse.success(categoryBaseVO);
    }

    @GetMapping("/getCategoryList")
    public List<Map> getCategoryList(){

        return categoryService.getCategoryList();
    }

    /**
     * 返回所有一级分类、当前分类以及其所有2级分类信息
     * @return
     */
    @GetMapping("/index")
    public CommonResponse index(@RequestParam("id") Long id){
        return categoryService.wxIndex(id);
    }

    /**
     * 当前一级分类的2级分类信息
     * @param id 一级分类id
     * @return
     */
    @GetMapping("/current")
    public CommonResponse current(@RequestParam("id") Long id){
        return categoryService.wxCurrent(id);
    }
}